﻿using System;
using Microsoft.Extensions.Configuration;

namespace iTeamyou.LTS.Core.Services
{
	public class SharedService
	{
        public event Action RefreshRequested;
        private string? AppPath;

        public SharedService()
        {
            if (string.IsNullOrEmpty(AppContext.BaseDirectory))
                AppPath = Directory.GetCurrentDirectory();
            else
                AppPath = AppContext.BaseDirectory;
        }

        public void CallRequestRefresh()
        {
            RefreshRequested?.Invoke();
        }

        public string GetConfig(string name)
        {
            var cb = new ConfigurationBuilder().SetBasePath(AppPath).AddJsonFile("appsettings.json", false, reloadOnChange: true);
            var config = cb.Build();

            switch (name.ToLower())
            {
                case "resourcepath":
                    if (string.IsNullOrEmpty(config["LTSConfiguration:ResourcePath"]))
                        return Path.Combine(Directory.GetCurrentDirectory(), "resources");
                    return config["LTSConfiguration:ResourcePath"];
                //case "storetablename":
                //    if (string.IsNullOrEmpty(config["LTSConfiguration:StoreTableName"]))
                //        return "ekp_se_stores_master";
                //    return config["LTSConfiguration:StoreTableName"];
                case "resourceurl":
                    if (string.IsNullOrEmpty(config["LTSConfiguration:ResourceURL"]))
                        return "http://localhost:5380/files/";
                    return config["LTSConfiguration:ResourceURL"];
                case "exportpath":
                    if (string.IsNullOrEmpty(config["LTSConfiguration:ExportPath"]))
                        return Path.Combine(Directory.GetCurrentDirectory(), "excel");
                    return config["LTSConfiguration:ExportPath"];
                case "certfilename":
                    return config["LTSConfiguration:CertFileName"];
                case "certpassword":
                    return config["LTSConfiguration:CertPassword"];
                case "maxuploadsize":
                    return config["LTSConfiguration:MaxUploadSize"].ToString();
                default:
                    if (string.IsNullOrEmpty(config[$"LTSConfiguration:{name}"]))
                        return string.Empty;
                    return config[$"LTSConfiguration:{name}"];
            }
        }

        public string GetConnectionString(string name)
        {
            if (File.Exists(Path.Combine(AppPath, "appsettings.json")))
            {
                var cb = new ConfigurationBuilder().SetBasePath(AppPath).AddJsonFile("appsettings.json", false, reloadOnChange: true);
                var config = cb.Build();

                return config[$"ConnectionStrings:{name}"];
            }
            else
            {
                switch (name.ToLower())
                {
                    case "maindb":
                        return "server=localhost; userid=root; password=111qqq!; port=3306; database=lts; sslmode=Required;";
                    case "ssodb":
                        return "server=localhost; userid=root; password=111qqq!; port=3306; database=sso; sslmode=Required;";
                    default:
                        return string.Empty;
                } 
            }
        }

        public object GetPropertyValue(object obj, string property)
        {
            var propertyInfo = obj.GetType().GetProperty(property);
            return propertyInfo.GetValue(obj, null);
        }
    }
}

